package com.gx.smart.building;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.app.sysuser.v1.SysUserApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.app.sysuser.v1.SysUserDto;
import com.huishi.gx.ib.grpc.lib.ib.app.sysuser.v1.SysUserLoginResp;
import com.huishi.gx.ib.grpc.lib.ib.app.sysuser.v1.SysUserQueryResponse;
import com.orhanobut.logger.Logger;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/3/23
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class SysUserApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final SysUserApiService INSTANCE = new SysUserApiService();
    }

    public static SysUserApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private SysUserApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private SysUserApiGrpc.SysUserApiBlockingStub getStub(ManagedChannel channel) {
        return SysUserApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 登陆
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, SysUserLoginResp> login(CallBack<SysUserLoginResp> callBack) {
        return new GrpcAsyncTask<String, Void, SysUserLoginResp>(callBack) {
            @Override
            protected SysUserLoginResp doRequestData(ManagedChannel channel) {
                SysUserDto message = SysUserDto.newBuilder()
                        .build();
                Logger.d(message);
                SysUserLoginResp response = null;
                try {
                    response = getStub(channel).login(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "login");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 查询
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, SysUserQueryResponse> sysUserQuery(
            List<String> organId,
            CallBack<SysUserQueryResponse> callBack) {
            return new GrpcAsyncTask<String, Void, SysUserQueryResponse>(callBack) {
                @Override
                protected SysUserQueryResponse doRequestData(ManagedChannel channel) {
                    SysUserDto message = SysUserDto.newBuilder()
                            .setComQuery(ComQuery
                                    .newBuilder()
                                    .setPage(1)
                                    .setPageSize(10000)
                                    .build())
                            .addAllOrganId(organId)
                            .build();
                    Logger.d(message);
                    SysUserQueryResponse response = null;
                    try {
                        response = getStub(channel).sysUserQuery(message);
                        Logger.d(response);
                    } catch (Exception e) {
                        Logger.e(e, "sysUserQuery");
                    }
                    return response;
                }
            }.setHost(host).setPort(port).doExecute();
        }

    /**
     * 查询
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, SysUserQueryResponse> sysUserQuery(
            String name,
            CallBack<SysUserQueryResponse> callBack) {
        return new GrpcAsyncTask<String, Void, SysUserQueryResponse>(callBack) {
            @Override
            protected SysUserQueryResponse doRequestData(ManagedChannel channel) {
                SysUserDto message = SysUserDto.newBuilder()
                        .setComQuery(ComQuery
                                .newBuilder()
                                .setPage(1)
                                .setPageSize(10000)
                                .build())
                        .setName(name)
                        .build();
                Logger.d(message);
                SysUserQueryResponse response = null;
                try {
                    response = getStub(channel).sysUserQuery(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "sysUserQuery");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

}
